package com.keda.model;

import java.io.Serializable;
import java.util.List;

/**
 * 分页结果对象
 */
public class PageInfo<T> implements Serializable {

    private static final int DEFAULT_PAGE_SIZE = 8;

    private static final int DEFAULT_PAGE_NUM = 7;

    // 当前页码
    private Integer page = 1;

    // 每页条数
    private Integer pageSize = DEFAULT_PAGE_SIZE;

    // 总记录数
    private Integer totalCount;

    // 总页数
    private Integer totalPages;

    // 查询结果
    private List<T> rows;

    private int[] navigateNums;

    public Integer getPage() {
        // 默认第1页
        return page == null ? 1 : page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getPageSize() {
        // 默认每页8条
        return pageSize == null ? DEFAULT_PAGE_SIZE : pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public Integer getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(Integer totalCount) {
        this.totalCount = totalCount;
    }

    public Integer getTotalPages() {
        // 计算总页数
        this.totalPages = totalCount % this.pageSize == 0 ? totalCount/this.pageSize : totalCount/this.pageSize + 1 ;
        return totalPages;
    }

    public void setTotalPages(Integer totalPages) {
        this.totalPages = totalPages;
    }

    public List<T> getRows() {
        return rows;
    }

    public void setRows(List<T> rows) {
        this.rows = rows;
    }

    public int[] getNavigateNums() {
        // 构建分页导航数，如果导航数量太多，那么要进行省略，start和end的位置是通过中间位置决定
        // 假如一共只显示5个分页数字

        if (totalPages <= DEFAULT_PAGE_NUM) {
            navigateNums = new int[totalPages];
            for (int i = 0; i < totalPages; i++) {
                navigateNums[i] = i + 1;
            }
            return navigateNums;
        }

        navigateNums = new int[DEFAULT_PAGE_NUM];
        int startNum = this.page - DEFAULT_PAGE_NUM / 2;
        int endNum = this.page + DEFAULT_PAGE_NUM / 2;
        if (startNum < 1) { // 当起始页小于1时
            for(int i = 0; i < DEFAULT_PAGE_NUM; i++) {
                navigateNums[i] = i + 1;
            }
            return navigateNums;
        }
        // 当最大页大于总页数
        if (endNum > this.totalPages) {
            endNum = this.totalPages;
            for(int i = DEFAULT_PAGE_NUM - 1; i >= 0; --i) {
                navigateNums[i] = endNum--;
            }
            return navigateNums;
        }
        for(int i = 0; i < DEFAULT_PAGE_NUM; ++i) {
            navigateNums[i] = startNum + i;
        }
        return navigateNums;
    }

    public void setNavigateNums(int[] navigateNums) {
        this.navigateNums = navigateNums;
    }

    public PageInfo() {
    }

    public PageInfo(Integer page, Integer totalCount, List<T> rows) {
        this.page = page;
        this.pageSize = DEFAULT_PAGE_SIZE;
        this.totalCount = totalCount;
        this.rows = rows;
    }

    private PageInfo<T> buildPageInfo(Integer page, Integer pageSize,
                                      Integer totalCount, List<T> rows) {
        PageInfo<T> pageInfo = new PageInfo<>();
        pageInfo.setPage(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotalCount(totalCount);
        pageInfo.setRows(rows);
        return pageInfo;
    }

}
